# 汽车销售和投放广告销售预测
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from matplotlib import pyplot as plt

# 1.数据准备
dict_data = {
    "电视广告数(x)": [1, 3, 2, 1, 3],
    "汽车销售数(y)": [14, 24, 18, 17, 27]
}

# 2.数据预处理
df_data = pd.DataFrame(dict_data)
print(df_data)

# 2.1 预测值和特征值获取
x_data = df_data.iloc[:, 0].values.reshape(-1, 1)
y_data = df_data.iloc[:, 1].values

# 2.2 划分数据集
# x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.2, random_state=929)

# # 3.特征预处理
# transformer = StandardScaler()
# x_train = transformer.fit_transform(x_train)
# x_test = transformer.transform(x_test)

# 4.模型构建
model = LinearRegression()
model.fit(x_data, y_data)

y_pre = model.predict(x_data)
print("模型的斜率(权重)为", model.coef_)
print("模型的截距(偏置)为", model.intercept_)

# 5.绘制图像
plt.scatter(x_data, y_data, c='pink')
plt.plot(x_data,y_pre)
plt.show()